import Vue from 'vue'
import Router from 'vue-router'
import AddNews from '@/components/news/add-news/add-news'
import NewsList from '@/components/news/news-list/news-list'
import AdminList from '@/components/admin/admin-list/admin-list'
import AddAdmin from '@/components/admin/add-admin/add-admin'
import AddNewsType from '@/components/system/add-news-type/add-news-type'
import AddHomeType from '@/components/system/add-home-type/add-home-type'
import EditNewsType from '@/components/system/edit-news-type/edit-news-type'
import EditHomeType from '@/components/system/edit-home-type/edit-home-type'
import EditAdmin from '@/components/admin/edit-admin/edit-admin'
import EditNews from '@/components/news/edit-news/edit-news'
import EditHome from '@/components/home/edit-home/edit-home'
import NewsTypeList from '@/components/system/type-list/type-list'
import HomeTypeList from '@/components/system/home-type-list/home-type-list'
import BannerList from '@/components/banner/banner-list/banner-list'
import HomeList from '@/components/home/home-list/home-list'
import AddBanner from '@/components/banner/add-banner/add-banner'
import AddHome from '@/components/home/add-home/add-home'
import EditBanner from '@/components/banner/edit-banner/edit-banner'
import Login from '@/components/login/login'
import Home from '@/Home'
import store from '@/store/store'
import * as types from '@/store/types'
import axios from 'axios'

Vue.use(Router)

const routes = [
  {
    path: '/',
    redirect: '/getNewsList'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/',
        redirect: '/getNewsList'
      },
      {
        path: '/addNews',
        name: 'AddNews',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: AddNews
      },
      {
        path: '/getNewsList',
        name: 'NewsList',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: NewsList
      },
      {
        path: '/getAdminList',
        name: 'AdminList',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: AdminList
      },
      {
        path: '/addAdmin',
        name: 'AddAdmin',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: AddAdmin
      },
      {
        path: '/addNewsType',
        name: 'AddNewsType',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: AddNewsType
      },
      {
        path: '/getNewsTypeList',
        name: 'NewsTypeList',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: NewsTypeList
      },
      {
        path: '/getHomeTypeList',
        name: 'HomeTypeList',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: HomeTypeList
      },
      {
        path: '/editNewsType',
        name: 'EditNewsType',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: EditNewsType
      },
      {
        path: '/editHomeType',
        name: 'EditHomeType',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: EditHomeType
      },
      {
        path: '/editAdmin',
        name: 'EditAdmin',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: EditAdmin
      },
      {
        path: '/editNews',
        name: 'EditNews',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: EditNews
      },
      {
        path: '/editHome',
        name: 'EditHome',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: EditHome
      },
      {
        path: '/getBannerList',
        name: 'BannerList',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: BannerList
      },
      {
        path: '/getHomeList',
        name: 'HomeList',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: HomeList
      },
      {
        path: '/addBanner',
        name: 'AddBanner',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: AddBanner
      },
      {
        path: '/addHome',
        name: 'AddHome',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: AddHome
      },
      {
        path: '/editBanner',
        name: 'EditBanner',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: EditBanner
      },
      {
        path: '/addHomeType',
        name: 'AddHomeType',
        meta: {
          requireAuth: true// 添加该字段，表示进入这个路由是需要登录的
        },
        component: AddHomeType
      }
    ]
  }
]

// 页面刷新时，重新赋值token
if (window.localStorage.getItem('token')) {
  store.commit(types.LOGIN, window.localStorage.getItem('token'))
}

if (window.localStorage.getItem('username')) {
  store.commit(types.USERNAME, window.localStorage.getItem('username'))
}

const router = new Router({
  routes: routes
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(r => r.meta.requireAuth)) {
    axios.get('http://www.yuzhekeji.club:8080/login/userIsLogin', {
      params: {
        username: store.state.username
      }
    }).then(res => {
      let data = res.data
      if (data.status === 200) {
        if (store.state.token !== data.data) {
          store.state.token = ''
          store.state.usrname = ''
          next({
            path: '/login',
            query: {redirect: to.fullPath}
          })
        } else {
          store.commit(types.LOGIN, data.data)
          next()
        }
      } else {
        next({
          path: '/login',
          query: {redirect: to.fullPath}
        })
      }
    })
  } else {
    next()
  }
})

export default router
